<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="部门名称" prop="name" required>
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code" required>
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager" required>
        <el-select v-model="form.manager" placeholder="请选择部门负责人">
          <el-option
            v-for="obj in employees"
            :key="obj.id"
            :label="obj.username"
            :value="obj.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce" required>
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native="submitFn">确定</el-button>
        <el-button @click.native="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import {
  getEmployeeSimple,
  addDepartments,
  getDepartDetail,
  updateDepartments

} from '@/api/user'
export default {
  name: 'DeptDialog',
  props: {
    // 接收父亲传过来的部门id
    parentId: {
      type: [Number, String],
      required: true
    },
    // 编辑状态
    isEdit: {
      type: Boolean,
      required: true
    },
    // 父亲给的精简数据
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编号非重验证 +排除编辑时部门编号出现重复---------------验证编码
    const vaildCode = (rule, value, callback) => {
      let exitCodeList = this.originList.map(item => item.code)
      // 如果是编辑的话,需要进行移除校验
      if (this.isEdit) {
        exitCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      exitCodeList.includes(value) ? callback(new Error(`部门 ${value} 已经存在`)) : callback()
    }

    // 同级部门禁止出现重复部门------------------------------验证部门名称
    const vaildName = (rule, value, callback) => {
      let exitNameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)
      exitNameList.includes(value) ? callback(new Error(`部门 ${value} 已经存在`)) : callback()
      if (this.isEdit) {
        const dept = this.originList.find(item => item.id === this.parentId)
        const pid = dept.pid
        exitNameList = this.originList.filter(item => item.pid === pid && item.id !== this.parentId).map(item => item.name)
      }
    }

    return {
      form: {
        code: '', // 部门编码
        introduce: '', // 部门介绍
        manager: '', // 负责人名称
        name: ''// 部门名称
      },
      employees: [], // 下拉部门负责人
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: vaildName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: vaildCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmployeeSimpleFn() // 渲染下拉部门负责人
    this.getDepartDetailFn()// 调用当前部门数据
  },
  methods: {
    // 获取当前部门数据
    async  getDepartDetailFn() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.parentId)
        // console.log(res)
        this.form = res.data
      }
    },

    // 添加子部门
    async addDepartmentsFn() {
      const res = await addDepartments({ ...this.form, pid: this.parentId })
      console.log(res)
      this.$message({ type: 'success', message: '添加成功' })
      this.$emit('closeDialog')// 告诉父亲
    },
    // 编辑子部门
    async  editSondepentsFn() {
      const res = await updateDepartments(this.form)
      console.log(res)
      this.$message({ type: 'success', message: '编辑成功' })
      this.$emit('closeDialog')// 告诉父亲
    },

    // 渲染下拉部门负责人
    async getEmployeeSimpleFn() {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.employees = res.data
    },
    // 点击确定按钮
    submitFn() {
      // 确认用户点击的编辑还是添加
      this.$refs.form.validate(vaild => {
        if (!vaild) return
        this.isEdit ? this.editSondepentsFn() : this.addDepartmentsFn()
      })
    },
    // 取消按钮
    cancel() {
      this.$emit('shiftDialog')
    }

  }

}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
.el-form {
  padding: 0 60px 0 20px;
}
</style>
